package com.ruoyi.business.service.impl;

import java.util.*;

import com.ruoyi.business.domain.Indicator;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.IIndicatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.EmployeeScoreMapper;
import com.ruoyi.business.domain.EmployeeScore;
import com.ruoyi.business.service.IEmployeeScoreService;

/**
 * 员工积分Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-30
 */
@Service
public class EmployeeScoreServiceImpl  extends BaseServiceImpl implements IEmployeeScoreService
{
    @Autowired
    private EmployeeScoreMapper employeeScoreMapper;

    @Autowired
    private IIndicatorService indicatorService;

    /**
     * 查询员工积分
     * 
     * @param id 员工积分主键
     * @return 员工积分
     */
    @Override
    public EmployeeScore selectEmployeeScoreById(Long id)
    {
        return employeeScoreMapper.selectEmployeeScoreById(id);
    }

    /**
     * 查询员工积分列表
     * 
     * @param employeeScore 员工积分
     * @return 员工积分
     */
    @Override
    public List<EmployeeScore> selectEmployeeScoreList(EmployeeScore employeeScore)
    {
        return employeeScoreMapper.selectEmployeeScoreList(employeeScore);
    }

    /**
     * 新增员工积分
     * 
     * @param employeeScore 员工积分
     * @return 结果
     */
    @Override
    public int insertEmployeeScore(EmployeeScore employeeScore)
    {
        employeeScore.setCreateTime(DateUtils.getNowDate());
        return employeeScoreMapper.insertEmployeeScore(employeeScore);
    }

    /**
     * 修改员工积分
     * 
     * @param employeeScore 员工积分
     * @return 结果
     */
    @Override
    public int updateEmployeeScore(EmployeeScore employeeScore)
    {
        employeeScore.setUpdateTime(DateUtils.getNowDate());
        return employeeScoreMapper.updateEmployeeScore(employeeScore);
    }

    /**
     * 批量删除员工积分
     * 
     * @param ids 需要删除的员工积分主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeScoreByIds(Long[] ids)
    {
        return employeeScoreMapper.deleteEmployeeScoreByIds(ids);
    }

    /**
     * 删除员工积分信息
     * 
     * @param id 员工积分主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeScoreById(Long id)
    {
        return employeeScoreMapper.deleteEmployeeScoreById(id);
    }

    /**
     * 获取柱状图数据
     * @param employeeScore
     * @return* *  source: [
     *      *      *             ['product', '2015', '2016'],
     *      *      *             ['五星级', 43.3, 85.8],
     *      *      *             ['四星级', 83.1, 73.4],
     *      *      *             ['三星级', 86.4, 65.2],
     *      *      *             ['二星级', 72.4, 53.9],
     *      *      *       ] 返回这个结构
     */
    @Override
    public List<List<Object>> getBarData(EmployeeScore employeeScore) {

        List<List<Object>> result = new ArrayList<>();

        // 添加表头
        List<Object> header = new ArrayList<>();
        header.add("product");

        // 获取所有年份
        List<String> allYears = employeeScoreMapper.getAllYears(employeeScore);
        header.addAll(allYears);
        result.add(header);



        // 查询数据
        List<Map<String, Object>> list = employeeScoreMapper.getBarData(employeeScore);

        // 构建结果
        Map<String, List<Double>> dataMap = new LinkedHashMap<>();

        dataMap.put("二星级", new ArrayList<>(Collections.nCopies(allYears.size(), 0.0)));
        dataMap.put("三星级", new ArrayList<>(Collections.nCopies(allYears.size(), 0.0)));
        dataMap.put("四星级", new ArrayList<>(Collections.nCopies(allYears.size(), 0.0)));
        dataMap.put("五星级", new ArrayList<>(Collections.nCopies(allYears.size(), 0.0)));

        for (String year : allYears) {
            //dataMap.put(year, new ArrayList<>());
        }

        for (Map<String, Object> row : list) {
            String product = (String) row.get("product");
            String awardYear = (String) row.get("award_year");
            int peoples = ((Number) row.get("peoples")).intValue();

            if (!dataMap.containsKey(product)) {
                dataMap.put(product, new ArrayList<>(Collections.nCopies(allYears.size(), 0.0)));
            }

            int index = allYears.indexOf(awardYear);
            if (index >= 0) {
                List<Double> values = dataMap.get(product);
                values.set(index, (double) peoples);
            }
        }

        // 将数据添加到结果列表中
        for (Map.Entry<String, List<Double>> entry : dataMap.entrySet()) {
            List<Object> row = new ArrayList<>();
            row.add(entry.getKey());
            row.addAll(entry.getValue());
            result.add(row);
        }

        return result;
    }

    @Override
    public List<Map<String, Object>> getPieData(EmployeeScore employeeScore) {

        List<Map<String, Object>> list = employeeScoreMapper.getPieData(employeeScore);


        return list;
    }

    @Override
    public List<List<Object>> getPersonalData(EmployeeScore employeeScore) {
        List<List<Object>> result = new ArrayList<>();

        // 添加表头
        List<Object> header = new ArrayList<>();
        header.add("product");

        // 获取所有年份
        List<String> allYears = employeeScoreMapper.getPersonalDataYears(employeeScore);
        header.addAll(allYears);
        result.add(header);

        // 查询数据
        List<Map<String, Object>> list = employeeScoreMapper.getPersonalData(employeeScore);

        // 构建结果
        LinkedHashMap<String, List<Double>> dataMap = new LinkedHashMap<>();


        Indicator s = new Indicator();
        s.setType("-1");
        ArrayList<Indicator> IndicatorList = indicatorService.selectIndicatorList(s);
        for (Indicator indicator : IndicatorList) {
            dataMap.put(indicator.getEvaluationMetricName(), new ArrayList<>(Collections.nCopies(allYears.size(), 0.0)));
        }



        for (String year : allYears) {
            //dataMap.put(year, new ArrayList<>());
        }

        for (Map<String, Object> row : list) {
            String product = (String) row.get("product");
            String awardYear = (String) row.get("award_year");
            double peoples = ((Number) row.get("peoples")).doubleValue();

            if (!dataMap.containsKey(product)) {
                dataMap.put(product, new ArrayList<>(Collections.nCopies(allYears.size(), 0.0)));
            }

            int index = allYears.indexOf(awardYear);
            if (index >= 0) {
                List<Double> values = dataMap.get(product);
                values.set(index, peoples);
            }
        }

        // 将数据添加到结果列表中
        for (Map.Entry<String, List<Double>> entry : dataMap.entrySet()) {
            List<Object> row = new ArrayList<>();
            row.add(entry.getKey());
            row.addAll(entry.getValue());
            result.add(row);
        }

        return result;
    }

    @Override
    public List<Map<String, Object>> getRatingStar(EmployeeScore employeeScore) {
        List<Map<String, Object>> list = employeeScoreMapper.getRatingStar(employeeScore);
        return list;
    }

}
